﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using COP;
using System.Collections;
using COP.Extractors;
using COP.Objects.Maps;

namespace COP.Objects.Config
{
    public delegate void CreateSingleMAPEventHandler();
    public abstract class ConfigBase : COPBase
    {

        public ConfigBase(TagType type, IExtractor extractor)
        {
            this.type = type;
            this.extractor = extractor;

        }

        public event CreateSingleMAPEventHandler CreateSingleMAP;

        TagType type;
        public TagType Type
        {
            get { return this.type; }
        }

        public IExtractor extractor;
        public IExtractor Extractor
        {
            get { return this.extractor; }
        }
    


        List<MAPBase> maps = null;
        public List<MAPBase> Maps
        {
            get
            {
                if (this.maps == null)
                    maps = new List<MAPBase>();
                return this.maps;
            }
            set { this.maps = value; }
        }

        SourceType source;
        public SourceType Source
        {
            get { return this.source; }
            set { this.source = value; }
        }

        protected MAPBase mapToCreate = new MAP();
        protected string key = "";

        protected abstract string CreateKey(object config);
        protected abstract IEnumerable ExtractSource(IEnumerable config);
        protected abstract List<MAPBase> CreateMAPCollection(IEnumerable node);

        protected virtual bool IsConfigKeyMandate
        {
            get { return true; }
        }

        public void SetMAPToCreate(MAPBase baseMAP) 
        {
            this.mapToCreate = baseMAP;
        }

        public void Create(IEnumerable data)
        {
            if (IsConfigKeyMandate)
            {
                key = this.CreateKey(data);
                if (string.IsNullOrEmpty(key))
                    throw new Exception(Constant.Error.KeyRequired);
            }
            IEnumerable emunData = this.ExtractSource(data);
            this.Maps.AddRange(this.CreateMAPCollection(emunData));
        }

        public virtual void AppendDelegate()
        {
            foreach (MAPBase map in this.maps)
            {
                map.AppendDelegate(base.caller);
            }
        }

        public virtual object PullValue()
        { return this.Maps; }

        

    }
}